//
//  BookPreviewVC.m
//  HelpThePoor
//
//  Created by ldc on 2018/4/20.
//  Copyright © 2018年 van7ish. All rights reserved.
//

#import "BookPreviewVC.h"
#import "BookPreviewMarkCell.h"

@interface BookPreviewVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UICollectionView  *previewTable;

@end

@implementation BookPreviewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIView *topV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 49)];
    topV.backgroundColor = COLOR_BOOKDETAILBGCOLOR;
    topV.userInteractionEnabled = YES;
    [self.view addSubview:topV];
    
    UIButton *okbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    okbtn.frame = CGRectMake(10, STATUS_BAR_HEIGHT, 60, 49-STATUS_BAR_HEIGHT);
    okbtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [okbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [okbtn setTitle:@"完成" forState:UIControlStateNormal];
    [okbtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [topV addSubview:okbtn];
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    //设置cell与cell之间的列距
    flowLayout.minimumInteritemSpacing = 24;
    //设置cell与cell之间的行距
    flowLayout.minimumLineSpacing = 10;
    
    _previewTable = [[UICollectionView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, SCREEN_W, SCREEN_H-NAVIGATION_BAR_HEIGHT) collectionViewLayout:flowLayout];
    _previewTable.delegate= self;
    _previewTable.dataSource = self;
    [self.view addSubview:_previewTable];
    [_previewTable registerClass:[BookPreviewMarkCell class] forCellWithReuseIdentifier:@"cell"];
}

-(void)back{
    [self.navigationController popViewControllerAnimated:NO];
}

-(void)dealloc{
    NSLog(@"BookPreviewVC dealloc");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark  设置CollectionView的组数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

#pragma mark  设置CollectionView每组所包含的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.native) {
        //有多少张图片就显示多少页
        return [self.dataArr count];
    } else {
        return self.pages;
    }
    
}

#pragma mark  设置CollectionCell的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    BookPreviewMarkCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    if (self.native) {
        cell.bookImage.image = [UIImage imageWithContentsOfFile:[self.filePath stringByAppendingPathComponent:self.dataArr[indexPath.row]]];
    } else {
        [cell.bookImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@/%ld.jpg", KHostAddress, self.filePath, (long)(indexPath.row + 1)]] placeholderImage:ImageNamed(@"placeholdershuji")];
    }
    
    NSString *key = nil;
    if (_isMagazine) {
        key = [NSString stringWithFormat:@"%@_%@_%@",self.dataDic[@"issuename"],self.dataDic[@"issueid"],@(indexPath.row)];
    }
    else{
        key = [NSString stringWithFormat:@"%@_%@_%@",self.dataDic[@"name"],self.dataDic[@"id"],@(indexPath.row)];
    }
    
    NSString *str = [Tool getValueFromKey:key];
    if (str&&[str length] > 0) {
        cell.markImage.hidden = NO;
    }
    else{
        cell.markImage.hidden = YES;
    }
    
    return cell;
}



#pragma mark  定义每个UICollectionView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    int sw = 12*6;
    int w = (SCREEN_W-sw)/3;
    int h = w*1.4;
    return  CGSizeMake(w,h);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 10, 0, 10);//分别为上、左、下、右
}

#pragma mark  点击CollectionView触发事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.gotoMarkpage) {
        self.gotoMarkpage(indexPath.row);
    }
    [self.navigationController popViewControllerAnimated:NO];
}

#pragma mark  设置CollectionViewCell是否可以被点击
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

@end
